package com.yifi.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseController;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.common.utils.ToolString;
import com.yifi.common.vo.TableDataInfo;

import cn.hutool.core.util.StrUtil;

@Path(value="/syspost",viewPath = "/")
public class SysPostController extends BaseController {

	public void index()
	{
		String c_pro = ToolSession.getUser(this).getC_pro();
		set("c_pro", c_pro);
		render("system/syspost.html");
	}
	
	
	public void list()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		
		String cname=getPara("cname","");
		String sql="select * from mis_post where 1=1 ";
		if(StrUtil.isNotEmpty(cname))
		{
			sql+=" and cname like '%"+cname+"%' ";
		}
		if(StrUtil.isNotEmpty(c_pro))
		{
			sql+=" and c_pro='"+c_pro+"' ";
		}
		
		sql+=" order by isort,ccode ";
		
		List<Record> list=Db.find(sql);
		
		TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(list.size());
        rspData.setTotalPage(1);
        
        renderJson(rspData);
	}
	
	public void getData()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		
		String ccode=getPara("ccode","");
		String sql="select * from mis_post where ccode='"+ccode+"'";
		Record record=Db.findFirst(sql);
		renderJson(record);
	}
	
	public void save()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		String ccode=getPara("ccode","");
		String cname=getPara("cname","");
		String cdesc=getPara("cdesc","");
		String isort=getPara("isort","");

		String ctype=getPara("ctype","");
		
		String sql="select * from mis_post where ccode='"+ccode+"' ";
		Record record=Db.findFirst(sql);
		boolean isedit=true;
		if(record==null)
		{
			isedit=false;
			record=new Record();
			record.set("ccode", ccode);
			record.set("c_pro", c_pro);
		}
		else
		{
			if(!ctype.equals("edit"))
			{
				renderJson(error("岗位编码重复！"));
				return;
			}
		}
		record.set("cname", cname);
		record.set("cdesc", cdesc);
		record.set("isort", isort);
		
		if(DbUtils.save("mis_post", "ccode", record, isedit))
		{
			renderJson(success("保存成功！"));
		}
		else
		{
			renderJson(error("保存失败！"));
		}
	}
	
	public void remove()
	{
		String ids=getPara("ids","");
		ids=ToolString.getIdsToStr(ids);
		
		String sql="select count(1) from mis_user_post where cpost in ("+ids+") ";
		if(Db.queryInt(sql)>0)
		{
			renderJson(error("删除失败，请清空当前组！"));
			return;
		}
		
		sql="delete from mis_post where ccode in ("+ids+") ";
		if(Db.update(sql)>0)
		{
			renderJson(success("删除成功！"));
		}
		else
		{
			renderJson(error("删除失败！"));
		}
	}
	
	public void userlist()
	{
		String cpost=getPara("cpost","");

		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		String cname=getPara("cname","");
		String sql="select t2.*,(select cname from mis_org where ccode=t2.cdept) as deptname from mis_user_post t1,mis_user t2 where t1.ccode=t2.ccode ";

		if(StrUtil.isNotEmpty(cname))
		{
			sql+=" and t2.cname like '%"+cname+"%' ";
		}

		if(StrUtil.isNotEmpty(c_pro))
		{
			sql+=" and t1.c_pro='"+c_pro+"' ";
		}
		sql+=" and t1.cpost='"+cpost+"' order by t1.ccode ";
		
		List<Record> list=Db.find(sql);
		
		TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(list.size());
        rspData.setTotalPage(1);
        
        renderJson(rspData);
	}
	
	public void removeuser()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		String cpost=getPara("cpost","");
		String ids=getPara("ids","");
		ids=ToolString.getIdsToStr(ids);
		
		String sql="delete from mis_user_post where cpost='"+cpost+"' and ccode in ("+ids+") ";

		if(StrUtil.isNotEmpty(c_pro))
		{
			sql+=" and c_pro='"+c_pro+"' ";
		}
		if(Db.update(sql)>0)
		{
			renderJson(success("删除成功！"));
		}
		else
		{
			renderJson(error("删除失败！"));
		}
	}
	
	public void adduser()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		String cpost=getPara("cpost","");
		String ids=getPara("ids","");
		ids=ToolString.getIdsToStr(ids);
		
		String sql="select * from mis_user where ccode in ("+ids+")";

		if(StrUtil.isNotEmpty(c_pro))
		{
			sql+=" and c_pro='"+c_pro+"' ";
		}
		List<Record> list=Db.find(sql);
		List<String> listSql=new ArrayList<String>();
		for (Record record : list) {
			
			sql="insert mis_user_post (ccode,cpost,cflag,c_pro) values ('"+record.getStr("ccode")+"','"+cpost+"','1','"+c_pro+"')";
			listSql.add(sql);
		}
		
		if(Db.batch(listSql, listSql.size()).length>0)
		{
			renderJson(success("添加成功！"));
		}
		else
		{
			renderJson(error("添加失败！"));
		}
	}
}
